home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perltrap.Z / perltrap
Encoding:
Text File  |  1998-10-28  |  51.4 KB  |  1,849 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perltrap - Perl traps    for the    unwary
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.       The biggest trap of all is forgetting    to use the ----wwww switch;
  13.       see the _p_e_r_l_r_u_n manpage.  The    second biggest trap is not
  14.       making your entire program runnable under use    strict.     The
  15.       third    biggest    trap is    not reading the    list of    changes    in
  16.       this version of Perl;    see the    _p_e_r_l_d_e_l_t_a manpage.
  17.  
  18.       AAAAwwwwkkkk TTTTrrrraaaappppssss
  19.  
  20.       Accustomed aaaawwwwkkkk users should take special note    of the
  21.       following:
  22.  
  23.       +o   The English module, loaded via
  24.  
  25.           use English;
  26.  
  27.           allows you to refer to special variables (like $/) with
  28.           names (like $RS),    as though they were in aaaawwwwkkkk; see    the
  29.           _p_e_r_l_v_a_r manpage for details.
  30.  
  31.       +o   Semicolons are required after all    simple statements in
  32.           Perl (except at the end of a block).  Newline is not a
  33.           statement    delimiter.
  34.  
  35.       +o   Curly brackets are required on ifs and whiles.
  36.  
  37.       +o   Variables    begin with "$",    "@" or "%" in Perl.
  38.  
  39.       +o   Arrays index from    0.  Likewise string positions in
  40.           _s_u_b_s_t_r() and _i_n_d_e_x().
  41.  
  42.       +o   You have to decide whether your array has    numeric    or
  43.           string indices.
  44.  
  45.       +o   Hash values do not spring    into existence upon mere
  46.           reference.
  47.  
  48.       +o   You have to decide whether you want to use string    or
  49.           numeric comparisons.
  50.  
  51.       +o   Reading an input line does not split it for you.    You
  52.           get to split it to an array yourself.  And the _s_p_l_i_t()
  53.           operator has different arguments than aaaawwwwkkkk's.
  54.  
  55.       +o   The current input    line is    normally in $_,    not $0.     It
  56.           generally    does not have the newline stripped.  ($0 is
  57.           the name of the program executed.)  See the _p_e_r_l_v_a_r
  58.           manpage.
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  71.  
  72.  
  73.  
  74.       +o   $<_d_i_g_i_t> does not    refer to fields--it refers to
  75.           substrings matched by the    last match pattern.
  76.  
  77.       +o   The _p_r_i_n_t() statement does not add field and record
  78.           separators unless    you set    $, and $\.  You    can set    $OFS
  79.           and $ORS if you're using the English module.
  80.  
  81.       +o   You must open your files before you print    to them.
  82.  
  83.       +o   The range    operator is "..", not comma.  The comma
  84.           operator works as    in C.
  85.  
  86.       +o   The match    operator is "=~", not "~".  ("~" is the    one's
  87.           complement operator, as in C.)
  88.  
  89.       +o   The exponentiation operator is "**", not "^".  "^" is
  90.           the XOR operator,    as in C.  (You know, one could get the
  91.           feeling that aaaawwwwkkkk is basically incompatible with C.)
  92.  
  93.       +o   The concatenation    operator is ".", not the null string.
  94.           (Using the null string would render /pat/    /pat/
  95.           unparsable, because the third slash would    be interpreted
  96.           as a division operator--the tokenizer is in fact
  97.           slightly context sensitive for operators like "/", "?",
  98.           and ">".    And in fact, "." itself    can be the beginning
  99.           of a number.)
  100.  
  101.       +o   The next,    exit, and continue keywords work differently.
  102.  
  103.       +o   The following variables work differently:
  104.  
  105.             Awk          Perl
  106.             ARGC      $#ARGV or    scalar @ARGV
  107.             ARGV[0]   $0
  108.             FILENAME  $ARGV
  109.             FNR          $. - something
  110.             FS          (whatever    you like)
  111.             NF          $#Fld, or    some such
  112.             NR          $.
  113.             OFMT      $#
  114.             OFS          $,
  115.             ORS          $\
  116.             RLENGTH   length($&)
  117.             RS          $/
  118.             RSTART    length($`)
  119.             SUBSEP    $;
  120.  
  121.  
  122.       +o   You cannot set $RS to a pattern, only a string.
  123.  
  124.       +o   When in doubt, run the aaaawwwwkkkk construct through aaaa2222pppp and see
  125.           what it gives you.
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  137.  
  138.  
  139.  
  140.       CCCC TTTTrrrraaaappppssss
  141.  
  142.       Cerebral C programmers should    take note of the following:
  143.  
  144.       +o   Curly brackets are required on if's and while's.
  145.  
  146.       +o   You must use elsif rather    than else if.
  147.  
  148.       +o   The break    and continue keywords from C become in Perl
  149.           last and next, respectively.  Unlike in C, these do _N_O_T
  150.           work within a do { } while construct.
  151.  
  152.       +o   There's no switch    statement.  (But it's easy to build
  153.           one on the fly.)
  154.  
  155.       +o   Variables    begin with "$",    "@" or "%" in Perl.
  156.  
  157.       +o   printf() does not    implement the "*" format for
  158.           interpolating field widths, but it's trivial to use
  159.           interpolation of double-quoted strings to    achieve    the
  160.           same effect.
  161.  
  162.       +o   Comments begin with "#", not "/*".
  163.  
  164.       +o   You can't    take the address of anything, although a
  165.           similar operator in Perl is the backslash, which creates
  166.           a    reference.
  167.  
  168.       +o   ARGV must    be capitalized.     $ARGV[0] is C's argv[1], and
  169.           argv[0] ends up in $0.
  170.  
  171.       +o   System calls such    as _l_i_n_k(), _u_n_l_i_n_k(), _r_e_n_a_m_e(), etc.
  172.           return nonzero for success, not 0.
  173.  
  174.       +o   Signal handlers deal with    signal names, not numbers.
  175.           Use kill -l to find their    names on your system.
  176.  
  177.       SSSSeeeedddd TTTTrrrraaaappppssss
  178.  
  179.       Seasoned sssseeeedddd programmers should take note of the following:
  180.  
  181.       +o   Backreferences in    substitutions use "$" rather than "\".
  182.  
  183.       +o   The pattern matching metacharacters "(", ")", and    "|" do
  184.           not have backslashes in front.
  185.  
  186.       +o   The range    operator is ..., rather    than comma.
  187.  
  188.       SSSShhhheeeellllllll    TTTTrrrraaaappppssss
  189.  
  190.       Sharp    shell programmers should take note of the following:
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  203.  
  204.  
  205.  
  206.       +o   The backtick operator does variable interpolation
  207.           without regard to    the presence of    single quotes in the
  208.           command.
  209.  
  210.       +o   The backtick operator does no translation    of the return
  211.           value, unlike ccccsssshhhh.
  212.  
  213.       +o   Shells (especially ccccsssshhhh) do several levels    of
  214.           substitution on each command line.  Perl does
  215.           substitution in only certain constructs such as double
  216.           quotes, backticks, angle brackets, and search patterns.
  217.  
  218.       +o   Shells interpret scripts a little    bit at a time.    Perl
  219.           compiles the entire program before executing it (except
  220.           for BEGIN    blocks,    which execute at compile time).
  221.  
  222.       +o   The arguments are    available via @ARGV, not $1, $2, etc.
  223.  
  224.       +o   The environment is not automatically made    available as
  225.           separate scalar variables.
  226.  
  227.       PPPPeeeerrrrllll TTTTrrrraaaappppssss
  228.  
  229.       Practicing Perl Programmers should take note of the
  230.       following:
  231.  
  232.       +o   Remember that many operations behave differently in a
  233.           list context than    they do    in a scalar one.  See the
  234.           _p_e_r_l_d_a_t_a manpage for details.
  235.  
  236.       +o   Avoid barewords if you can, especially all lowercase
  237.           ones.  You can't tell by just looking at it whether a
  238.           bareword is a function or    a string.  By using quotes on
  239.           strings and parentheses on function calls, you won't
  240.           ever get them confused.
  241.  
  242.       +o   You cannot discern from mere inspection which builtins
  243.           are unary    operators (like    _c_h_o_p() and _c_h_d_i_r()) and    which
  244.           are list operators (like _p_r_i_n_t() and _u_n_l_i_n_k()).  (User-
  245.           defined subroutines can be oooonnnnllllyyyy list operators, never
  246.           unary ones.)  See    the _p_e_r_l_o_p manpage.
  247.  
  248.       +o   People have a hard time remembering that some functions
  249.           default to $_, or    @ARGV, or whatever, but    that others
  250.           which you    might expect to    do not.
  251.  
  252.       +o   The <FH> construct is not    the name of the    filehandle, it
  253.           is a readline operation on that handle.  The data    read
  254.           is assigned to $_    only if    the file read is the sole
  255.           condition    in a while loop:
  256.  
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  269.  
  270.  
  271.  
  272.           while    (<FH>)        { }
  273.           while    (defined($_ = <FH>)) { }..
  274.           <FH>;     # data    discarded!
  275.  
  276.  
  277.       +o   Remember not to use "=" when you need "=~"; these    two
  278.           constructs are quite different:
  279.  
  280.           $x =    /foo/;
  281.           $x =~    /foo/;
  282.  
  283.  
  284.       +o   The do {}    construct isn't    a real loop that you can use
  285.           loop control on.
  286.  
  287.       +o   Use my() for local variables whenever you    can get    away
  288.           with it (but see the _p_e_r_l_f_o_r_m manpage for    where you
  289.           can't).  Using local() actually gives a local value to a
  290.           global variable, which leaves you    open to    unforeseen
  291.           side-effects of dynamic scoping.
  292.  
  293.       +o   If you localize an exported variable in a    module,    its
  294.           exported value will not change.  The local name becomes
  295.           an alias to a new    value but the external name is still
  296.           an alias for the original.
  297.  
  298.       PPPPeeeerrrrllll4444    ttttoooo PPPPeeeerrrrllll5555 TTTTrrrraaaappppssss
  299.  
  300.       Practicing Perl4 Programmers should take note    of the
  301.       following Perl4-to-Perl5 specific traps.
  302.  
  303.       They're crudely ordered according to the following list:
  304.  
  305.       Discontinuance, Deprecation, and BugFix traps
  306.           Anything that's been fixed as a perl4 bug, removed as a
  307.           perl4 feature or deprecated as a perl4 feature with the
  308.           intent to    encourage usage    of some    other perl5 feature.
  309.  
  310.       Parsing Traps
  311.           Traps that appear    to stem    from the new parser.
  312.  
  313.       Numerical Traps
  314.           Traps having to do with numerical    or mathematical
  315.           operators.
  316.  
  317.       General data type traps
  318.           Traps involving perl standard data types.
  319.  
  320.       Context Traps    - scalar, list contexts
  321.           Traps related to context within lists, scalar
  322.           statements/declarations.
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  335.  
  336.  
  337.  
  338.       Precedence Traps
  339.           Traps related to the precedence of parsing, evaluation,
  340.           and execution of code.
  341.  
  342.       General Regular Expression Traps using s///, etc.
  343.           Traps related to the use of pattern matching.
  344.  
  345.       Subroutine, Signal, Sorting Traps
  346.           Traps related to the use of signals and signal handlers,
  347.           general subroutines, and sorting,    along with sorting
  348.           subroutines.
  349.  
  350.       OS Traps
  351.           OS-specific traps.
  352.  
  353.       DBM Traps
  354.           Traps specific to    the use    of dbmopen(), and specific dbm
  355.           implementations.
  356.  
  357.       Unclassified Traps
  358.           Everything else.
  359.  
  360.       If you find an example of a conversion trap that is not
  361.       listed here, please submit it    to Bill    Middleton
  362.       <_w_j_m@_b_e_s_t._c_o_m> for inclusion.     Also note that    at least some
  363.       of these can be caught with ----wwww.
  364.  
  365.       DDDDiiiissssccccoooonnnnttttiiiinnnnuuuuaaaannnncccceeee,,,, DDDDeeeepppprrrreeeeccccaaaattttiiiioooonnnn,,,, aaaannnndddd BBBBuuuuggggFFFFiiiixxxx ttttrrrraaaappppssss
  366.  
  367.       Anything that    has been discontinued, deprecated, or fixed as
  368.       a bug    from perl4.
  369.  
  370.       +o Discontinuance
  371.           Symbols starting with "_"    are no longer forced into
  372.           package main, except for $_ itself (and @_, etc.).
  373.  
  374.           package test;
  375.           $_legacy = 1;
  376.  
  377.           package main;
  378.           print    "\$_legacy is ",$_legacy,"\n";
  379.  
  380.           # perl4 prints: $_legacy is 1
  381.           # perl5 prints: $_legacy is
  382.  
  383.  
  384.       +o Deprecation
  385.           Double-colon is now a valid package separator in a
  386.           variable name.  Thus these behave    differently in perl4
  387.           vs. perl5, because the packages don't exist.
  388.  
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  401.  
  402.  
  403.  
  404.           $a=1;$b=2;$c=3;$var=4;
  405.           print    "$a::$b::$c ";
  406.           print    "$var::abc::xyz\n";
  407.  
  408.           # perl4 prints: 1::2::3 4::abc::xyz
  409.           # perl5 prints: 3
  410.  
  411.           Given that :: is now the preferred package delimiter, it
  412.           is debatable whether this    should be classed as a bug or
  413.           not.  (The older package delimiter, ' ,is    used here)
  414.  
  415.           $x = 10 ;
  416.           print    "x=${'x}\n" ;
  417.  
  418.           # perl4 prints: x=10
  419.           # perl5 prints: Can't    find string terminator "'" anywhere before EOF
  420.  
  421.           You can avoid this problem, and remain compatible    with
  422.           perl4, if    you always explicitly include the package
  423.           name:
  424.  
  425.           $x = 10 ;
  426.           print    "x=${main'x}\n"    ;
  427.  
  428.           Also see precedence traps, for parsing $:.
  429.  
  430.       +o BugFix
  431.           The second and third arguments of    splice() are now
  432.           evaluated    in scalar context (as the Camel    says) rather
  433.           than list    context.
  434.  
  435.           sub sub1{return(0,2) }      # return a 2-element list
  436.           sub sub2{ return(1,2,3)}      # return a 3-element list
  437.           @a1 =    ("a","b","c","d","e");
  438.           @a2 =    splice(@a1,&sub1,&sub2);
  439.           print    join(' ',@a2),"\n";
  440.  
  441.           # perl4 prints: a b
  442.           # perl5 prints: c d e
  443.  
  444.  
  445.       +o Discontinuance
  446.           You can't    do a goto into a block that is optimized away.
  447.           Darn.
  448.  
  449.           goto marker1;
  450.  
  451.           for(1){
  452.           marker1:
  453.               print "Here I is!\n";
  454.           }
  455.  
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  467.  
  468.  
  469.  
  470.           # perl4 prints: Here I is!
  471.           # perl5 dumps    core (SEGV)
  472.  
  473.  
  474.       +o Discontinuance
  475.           It is no longer syntactically legal to use whitespace as
  476.           the name of a variable, or as a delimiter    for any    kind
  477.           of quote construct.  Double darn.
  478.  
  479.           $a = ("foo bar");
  480.           $b = q baz ;
  481.           print    "a is $a, b is $b\n";
  482.  
  483.           # perl4 prints: a is foo bar,    b is baz
  484.           # perl5 errors: Bareword found where operator    expected
  485.  
  486.  
  487.       +o Discontinuance
  488.           The archaic while/if BLOCK BLOCK syntax is no longer
  489.           supported.
  490.  
  491.           if { 1 } {
  492.               print "True!";
  493.           }
  494.           else {
  495.               print "False!";
  496.           }
  497.  
  498.           # perl4 prints: True!
  499.           # perl5 errors: syntax error at test.pl line 1, near "if {"
  500.  
  501.  
  502.       +o BugFix
  503.           The ** operator now binds    more tightly than unary    minus.
  504.           It was documented    to work    this way before, but didn't.
  505.  
  506.           print    -4**2,"\n";
  507.  
  508.           # perl4 prints: 16
  509.           # perl5 prints: -16
  510.  
  511.  
  512.       +o Discontinuance
  513.           The meaning of foreach{} has changed slightly when it is
  514.           iterating    over a list which is not an array.  This used
  515.           to assign    the list to a temporary    array, but no longer
  516.           does so (for efficiency).     This means that you'll    now be
  517.           iterating    over the actual    values,    not over copies    of the
  518.           values.  Modifications to    the loop variable can change
  519.           the original values.
  520.  
  521.  
  522.  
  523.  
  524.  
  525.      Page 8                        (printed 10/23/98)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  533.  
  534.  
  535.  
  536.           @list    = ('ab','abc','bcd','def');
  537.           foreach $var (grep(/ab/,@list)){
  538.               $var = 1;
  539.           }
  540.           print    (join(':',@list));
  541.  
  542.           # perl4 prints: ab:abc:bcd:def
  543.           # perl5 prints: 1:1:bcd:def
  544.  
  545.           To retain    Perl4 semantics    you need to assign your    list
  546.           explicitly to a temporary    array and then iterate over
  547.           that.  For example, you might need to change
  548.  
  549.           foreach $var (grep(/ab/,@list)){
  550.  
  551.           to
  552.  
  553.           foreach $var (@tmp = grep(/ab/,@list)){
  554.  
  555.           Otherwise    changing $var will clobber the values of
  556.           @list.  (This most often happens when you    use $_ for the
  557.           loop variable, and call subroutines in the loop that
  558.           don't properly localize $_.)
  559.  
  560.       +o Discontinuance
  561.           split with no arguments now behaves like split ' '
  562.           (which doesn't return an initial null field if $_    starts
  563.           with whitespace),    it used    to behave like split /\s+/
  564.           (which does).
  565.  
  566.           $_ = ' hi mom';
  567.           print    join(':', split);
  568.  
  569.           # perl4 prints: :hi:mom
  570.           # perl5 prints: hi:mom
  571.  
  572.  
  573.       +o BugFix
  574.           Perl 4 would ignore any text which was attached to an ----eeee
  575.           switch, always taking the    code snippet from the
  576.           following    arg.  Additionally, it would silently accept
  577.           an ----eeee switch without a following arg.  Both of these
  578.           behaviors    have been fixed.
  579.  
  580.           perl -e'print    "attached to -e"' 'print "separate arg"'
  581.  
  582.           # perl4 prints: separate arg
  583.           # perl5 prints: attached to -e
  584.  
  585.           perl -e
  586.  
  587.  
  588.  
  589.  
  590.  
  591.      Page 9                        (printed 10/23/98)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  599.  
  600.  
  601.  
  602.           # perl4 prints:
  603.           # perl5 dies:    No code    specified for -e.
  604.  
  605.  
  606.       +o Discontinuance
  607.           In Perl 4    the return value of push was undocumented, but
  608.           it was actually the last value being pushed onto the
  609.           target list.  In Perl 5 the return value of push is
  610.           documented, but has changed, it is the number of
  611.           elements in the resulting    list.
  612.  
  613.           @x = ('existing');
  614.           print    push(@x, 'first    new', 'second new');
  615.  
  616.           # perl4 prints: second new
  617.           # perl5 prints: 3
  618.  
  619.  
  620.       +o Discontinuance
  621.           In Perl 4    (and versions of Perl 5    before 5.004), '\r'
  622.           characters in Perl code were silently allowed, although
  623.           they could cause (mysterious!)  failures in certain
  624.           constructs, particularly here documents.    Now, '\r'
  625.           characters cause an immediate fatal error.  (Note: In
  626.           this example, the    notation \\\\000011115555 represents the incorrect
  627.           line ending. Depending upon your text viewer, it will
  628.           look different.)
  629.  
  630.           print    "foo";\015
  631.           print    "bar";
  632.  
  633.           # perl4     prints: foobar
  634.           # perl5.003 prints: foobar
  635.           # perl5.004 dies: Illegal character \015 (carriage return)
  636.  
  637.           See the _p_e_r_l_d_i_a_g manpage for full    details.
  638.  
  639.       +o Deprecation
  640.           Some error messages will be different.
  641.  
  642.       +o Discontinuance
  643.           Some bugs    may have been inadvertently removed.  :-)
  644.  
  645.       PPPPaaaarrrrssssiiiinnnngggg TTTTrrrraaaappppssss
  646.  
  647.       Perl4-to-Perl5 traps from having to do with parsing.
  648.  
  649.       +o Parsing
  650.           Note the space between . and =
  651.  
  652.           $string . = "more string";
  653.           print    $string;
  654.  
  655.  
  656.  
  657.      Page 10                        (printed 10/23/98)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  665.  
  666.  
  667.  
  668.           # perl4 prints: more string
  669.           # perl5 prints: syntax error at - line 1, near ". ="
  670.  
  671.  
  672.       +o Parsing
  673.           Better parsing in    perl 5
  674.  
  675.           sub foo {}
  676.           &foo
  677.           print("hello,    world\n");
  678.  
  679.           # perl4 prints: hello, world
  680.           # perl5 prints: syntax error
  681.  
  682.  
  683.       +o Parsing
  684.           "if it looks like    a function, it is a function" rule.
  685.  
  686.         print
  687.           ($foo    == 1) ?    "is one\n" : "is zero\n";
  688.  
  689.           # perl4 prints: is zero
  690.           # perl5 warns: "Useless use of a constant in void context" if    using -w
  691.  
  692.  
  693.       +o Parsing
  694.           String interpolation of the $#array construct differs
  695.           when braces are to used around the name.
  696.  
  697.           @ = (1..3);
  698.           print    "${#a}";
  699.  
  700.           # perl4 prints: 2
  701.           # perl5 fails    with syntax error
  702.  
  703.           @ = (1..3);
  704.           print    "$#{a}";
  705.  
  706.           # perl4 prints: {a}
  707.           # perl5 prints: 2
  708.  
  709.  
  710.       NNNNuuuummmmeeeerrrriiiiccccaaaallll TTTTrrrraaaappppssss
  711.  
  712.       Perl4-to-Perl5 traps having to do with numerical operators,
  713.       operands, or output from same.
  714.  
  715.       +o Numerical
  716.            Formatted output    and significant    digits
  717.  
  718.            print 7.373504 - 0, "\n";
  719.            printf "%20.18f\n", 7.373504    - 0;
  720.  
  721.  
  722.  
  723.      Page 11                        (printed 10/23/98)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  731.  
  732.  
  733.  
  734.            # Perl4 prints:
  735.            7.375039999999996141
  736.            7.37503999999999614
  737.  
  738.            # Perl5 prints:
  739.            7.373504
  740.            7.37503999999999614
  741.  
  742.  
  743.       +o Numerical
  744.            This specific item has been deleted.  It    demonstrated
  745.            how the auto-increment operator would not catch when a
  746.            number went over    the signed int limit.  Fixed in
  747.            version 5.003_04.  But always be    wary when using    large
  748.            integers.  If in    doubt:
  749.  
  750.           use Math::BigInt;
  751.  
  752.  
  753.       +o Numerical
  754.            Assignment of return values from    numeric    equality tests
  755.            does not    work in    perl5 when the test evaluates to false
  756.            (0).  Logical tests now return an null, instead of 0
  757.  
  758.            $p =    ($test == 1);
  759.            print $p,"\n";
  760.  
  761.            # perl4 prints: 0
  762.            # perl5 prints:
  763.  
  764.            Also see    the section on _G_e_n_e_r_a_l _R_e_g_u_l_a_r _E_x_p_r_e_s_s_i_o_n
  765.            _T_r_a_p_s _u_s_i_n_g _s///, _e_t_c.  for another example of this new
  766.            feature...
  767.  
  768.       GGGGeeeennnneeeerrrraaaallll ddddaaaattttaaaa ttttyyyyppppeeee ttttrrrraaaappppssss
  769.  
  770.       Perl4-to-Perl5 traps involving most data-types, and their
  771.       usage    within certain expressions and/or context.
  772.  
  773.       +o (Arrays)
  774.            Negative    array subscripts now count from    the end    of the
  775.            array.
  776.  
  777.            @a =    (1, 2, 3, 4, 5);
  778.            print "The third element of the array is $a[3] also expressed as $a[-2] \n";
  779.  
  780.            # perl4 prints: The third element of    the array is 4 also expressed as
  781.            # perl5 prints: The third element of    the array is 4 also expressed as 4
  782.  
  783.  
  784.       +o (Arrays)
  785.            Setting $#array lower now discards array    elements, and
  786.  
  787.  
  788.  
  789.      Page 12                        (printed 10/23/98)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  797.  
  798.  
  799.  
  800.            makes them impossible to    recover.
  801.  
  802.            @a =    (a,b,c,d,e);
  803.            print "Before: ",join('',@a);
  804.            $#a =1;
  805.            print ", After: ",join('',@a);
  806.            $#a =3;
  807.            print ", Recovered: ",join('',@a),"\n";
  808.  
  809.            # perl4 prints: Before: abcde, After: ab, Recovered:    abcd
  810.            # perl5 prints: Before: abcde, After: ab, Recovered:    ab
  811.  
  812.  
  813.       +o (Hashes)
  814.            Hashes get defined before use
  815.  
  816.            local($s,@a,%h);
  817.            die "scalar \$s defined" if defined($s);
  818.            die "array \@a defined" if defined(@a);
  819.            die "hash \%h defined" if defined(%h);
  820.  
  821.            # perl4 prints:
  822.            # perl5 dies: hash %h defined
  823.  
  824.  
  825.       +o (Globs)
  826.            glob assignment from variable to    variable will fail if
  827.            the assigned variable is    localized subsequent to    the
  828.            assignment
  829.  
  830.            @a =    ("This is Perl 4");
  831.            *b =    *a;
  832.            local(@a);
  833.            print @b,"\n";
  834.  
  835.            # perl4 prints: This    is Perl    4
  836.            # perl5 prints:
  837.  
  838.  
  839.       +o (Globs)
  840.            Assigning undef to a glob has no    effect in Perl 5.   In
  841.            Perl 4 it undefines the associated scalar (but may have
  842.            other side effects including SEGVs).
  843.  
  844.       +o (Scalar String)
  845.            Changes in unary    negation (of strings) This change
  846.            effects both the    return value and what it does to
  847.            _a_u_t_o(magic)increment.
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.      Page 13                        (printed 10/23/98)
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  863.  
  864.  
  865.  
  866.            $x =    "aaa";
  867.            print ++$x,"    : ";
  868.            print -$x," : ";
  869.            print ++$x,"\n";
  870.  
  871.            # perl4 prints: aab : -0 : 1
  872.            # perl5 prints: aab : -aab :    aac
  873.  
  874.  
  875.       +o (Constants)
  876.            perl 4 lets you modify constants:
  877.  
  878.            $foo    = "x";
  879.            &mod($foo);
  880.            for ($x = 0;    $x < 3;    $x++) {
  881.                &mod("a");
  882.            }
  883.            sub mod {
  884.                print "before: $_[0]";
  885.                $_[0] = "m";
  886.                print "    after: $_[0]\n";
  887.            }
  888.  
  889.            # perl4:
  890.            # before: x    after: m
  891.            # before: a    after: m
  892.            # before: m    after: m
  893.            # before: m    after: m
  894.  
  895.            # Perl5:
  896.            # before: x    after: m
  897.            # Modification of a read-only value attempted at foo.pl line    12.
  898.            # before: a
  899.  
  900.  
  901.       +o (Scalars)
  902.            The behavior is slightly    different for:
  903.  
  904.            print "$x", defined $x
  905.  
  906.            # perl 4: 1
  907.            # perl 5: <no output, $x is not called into existence>
  908.  
  909.  
  910.       +o (Variable Suicide)
  911.            Variable    suicide    behavior is more consistent under Perl
  912.            5.  Perl5 exhibits the same behavior for    hashes and
  913.            scalars,    that perl4 exhibits for    only scalars.
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.      Page 14                        (printed 10/23/98)
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  929.  
  930.  
  931.  
  932.            $aGlobal{ "aKey" } =    "global    value";
  933.            print "MAIN:", $aGlobal{"aKey"}, "\n";
  934.            $GlobalLevel    = 0;
  935.            &test( *aGlobal );
  936.  
  937.            sub test {
  938.                local( *theArgument ) = @_;
  939.                local( %aNewLocal ); # perl 4 !=    5.001l,m
  940.                $aNewLocal{"aKey"} = "this should never appear";
  941.                print "SUB: ", $theArgument{"aKey"}, "\n";
  942.                $aNewLocal{"aKey"} = "level $GlobalLevel";   # what should print
  943.                $GlobalLevel++;
  944.                if( $GlobalLevel<4 ) {
  945.                &test( *aNewLocal );
  946.                }
  947.            }
  948.  
  949.            # Perl4:
  950.            # MAIN:global value
  951.            # SUB: global value
  952.            # SUB: level    0
  953.            # SUB: level    1
  954.            # SUB: level    2
  955.  
  956.            # Perl5:
  957.            # MAIN:global value
  958.            # SUB: global value
  959.            # SUB: this should never appear
  960.            # SUB: this should never appear
  961.            # SUB: this should never appear
  962.  
  963.  
  964.       CCCCoooonnnntttteeeexxxxtttt TTTTrrrraaaappppssss    ---- ssssccccaaaallllaaaarrrr,,,, lllliiiisssstttt ccccoooonnnntttteeeexxxxttttssss
  965.  
  966.       +o (list context)
  967.            The elements of argument    lists for formats are now
  968.            evaluated in list context.  This    means you can
  969.            interpolate list    values now.
  970.  
  971.            @fmt    = ("foo","bar","baz");
  972.            format STDOUT=
  973.            @<<<<< @||||| @>>>>>
  974.            @fmt;
  975.            .
  976.            write;
  977.  
  978.            # perl4 errors:  Please use commas to separate fields in file
  979.            # perl5 prints: foo       bar        baz
  980.  
  981.  
  982.       +o (scalar context)
  983.            The caller() function now returns a false value in a
  984.  
  985.  
  986.  
  987.      Page 15                        (printed 10/23/98)
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  995.  
  996.  
  997.  
  998.            scalar context if there is no caller.  This lets
  999.            library files determine if they're being    required.
  1000.  
  1001.            caller() ? (print "You rang?\n") : (print "Got a 0\n");
  1002.  
  1003.            # perl4 errors: There is no caller
  1004.            # perl5 prints: Got a 0
  1005.  
  1006.  
  1007.       +o (scalar context)
  1008.            The comma operator in a scalar context is now
  1009.            guaranteed to give a scalar context to its arguments.
  1010.  
  1011.            @y= ('a','b','c');
  1012.            $x =    (1, 2, @y);
  1013.            print "x = $x\n";
  1014.  
  1015.            # Perl4 prints:  x =    c   # Thinks list context interpolates list
  1016.            # Perl5 prints:  x =    3   # Knows scalar uses    length of list
  1017.  
  1018.  
  1019.       +o (list, builtin)
  1020.            sprintf() funkiness (array argument converted to    scalar
  1021.            array count) This test could be added to    t/op/sprintf.t
  1022.  
  1023.            @z =    ('%s%s', 'foo',    'bar');
  1024.            $x =    sprintf(@z);
  1025.            if ($x eq 'foobar') {print "ok 2\n";} else {print "not ok 2 '$x'\n";}
  1026.  
  1027.            # perl4 prints: ok 2
  1028.            # perl5 prints: not ok 2
  1029.  
  1030.            printf()    works fine, though:
  1031.  
  1032.            printf STDOUT (@z);
  1033.            print "\n";
  1034.  
  1035.            # perl4 prints: foobar
  1036.            # perl5 prints: foobar
  1037.  
  1038.            Probably    a bug.
  1039.  
  1040.       PPPPrrrreeeecccceeeeddddeeeennnncccceeee TTTTrrrraaaappppssss
  1041.  
  1042.       Perl4-to-Perl5 traps involving precedence order.
  1043.  
  1044.       Perl 4 has almost the    same precedence    rules as Perl 5    for
  1045.       the operators    that they both have.  Perl 4 however, seems to
  1046.       have had some    inconsistencies    that made the behavior differ
  1047.       from what was    documented.
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.      Page 16                        (printed 10/23/98)
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1061.  
  1062.  
  1063.  
  1064.       +o Precedence
  1065.            LHS vs. RHS of any assignment operator.    LHS is
  1066.            evaluated first in perl4, second    in perl5; this can
  1067.            affect the relationship between side-effects in sub-
  1068.            expressions.
  1069.  
  1070.            @arr    = ( 'left', 'right' );
  1071.            $a{shift @arr} = shift @arr;
  1072.            print join( ' ', keys %a );
  1073.  
  1074.            # perl4 prints: left
  1075.            # perl5 prints: right
  1076.  
  1077.  
  1078.       +o Precedence
  1079.            These are now semantic errors because of    precedence:
  1080.  
  1081.            @list = (1,2,3,4,5);
  1082.            %map    = ("a",1,"b",2,"c",3,"d",4);
  1083.            $n =    shift @list + 2;   # first item    in list    plus 2
  1084.            print "n is $n, ";
  1085.            $m =    keys %map + 2;       # number of items in    hash plus 2
  1086.            print "m is $m\n";
  1087.  
  1088.            # perl4 prints: n is    3, m is    6
  1089.            # perl5 errors and fails to compile
  1090.  
  1091.  
  1092.       +o Precedence
  1093.            The precedence of assignment operators is now the same
  1094.            as the precedence of assignment.     Perl 4    mistakenly
  1095.            gave them the precedence    of the associated operator.
  1096.            So you now must parenthesize them in expressions    like
  1097.  
  1098.            /foo/ ? ($a += 2) : ($a -= 2);
  1099.  
  1100.            Otherwise
  1101.  
  1102.            /foo/ ? $a += 2 : $a    -= 2
  1103.  
  1104.            would be    erroneously parsed as
  1105.  
  1106.            (/foo/ ? $a += 2 : $a) -= 2;
  1107.  
  1108.            On the other hand,
  1109.  
  1110.            $a += /foo/ ? 1 : 2;
  1111.  
  1112.            now works as a C    programmer would expect.
  1113.  
  1114.       +o Precedence
  1115.  
  1116.  
  1117.  
  1118.  
  1119.      Page 17                        (printed 10/23/98)
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1127.  
  1128.  
  1129.  
  1130.            open    FOO || die;
  1131.  
  1132.            is now incorrect.  You need parentheses around the
  1133.            filehandle.  Otherwise, perl5 leaves the    statement as
  1134.            its default precedence:
  1135.  
  1136.            open(FOO || die);
  1137.  
  1138.            # perl4 opens or dies
  1139.            # perl5 errors: Precedence problem: open FOO    should be open(FOO)
  1140.  
  1141.  
  1142.       +o Precedence
  1143.            perl4 gives the special variable, $: precedence,    where
  1144.            perl5 treats $::    as main    package
  1145.  
  1146.            $a =    "x"; print "$::a";
  1147.  
  1148.            # perl 4 prints: -:a
  1149.            # perl 5 prints: x
  1150.  
  1151.  
  1152.       +o Precedence
  1153.            perl4 had buggy precedence for the file test operators
  1154.            vis-a-vis the assignment    operators.  Thus, although the
  1155.            precedence table    for perl4 leads    one to believe -e $foo
  1156.            .= "q" should parse as ((-e $foo) .= "q"), it actually
  1157.            parses as (-e ($foo .= "q")).  In perl5,    the precedence
  1158.            is as documented.
  1159.  
  1160.            -e $foo .= "q"
  1161.  
  1162.            # perl4 prints: no output
  1163.            # perl5 prints: Can't modify    -e in concatenation
  1164.  
  1165.  
  1166.       +o Precedence
  1167.            In perl4, _k_e_y_s(), _e_a_c_h()    and _v_a_l_u_e_s() were special
  1168.            high-precedence operators that operated on a single
  1169.            hash, but in perl5, they    are regular named unary
  1170.            operators.  As documented, named    unary operators    have
  1171.            lower precedence    than the arithmetic and    concatenation
  1172.            operators + - .,    but the    perl4 variants of these
  1173.            operators actually bind tighter than + -    ..  Thus, for:
  1174.  
  1175.            %foo    = 1..10;
  1176.            print keys %foo - 1
  1177.  
  1178.            # perl4 prints: 4
  1179.            # perl5 prints: Type    of arg 1 to keys must be hash (not subtraction)
  1180.  
  1181.            The perl4 behavior was probably more useful, if less
  1182.  
  1183.  
  1184.  
  1185.      Page 18                        (printed 10/23/98)
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1193.  
  1194.  
  1195.  
  1196.            consistent.
  1197.  
  1198.       GGGGeeeennnneeeerrrraaaallll RRRReeeegggguuuullllaaaarrrr EEEExxxxpppprrrreeeessssssssiiiioooonnnn TTTTrrrraaaappppssss uuuussssiiiinnnngggg ssss////////////,,,, eeeettttcccc....
  1199.  
  1200.       All types of RE traps.
  1201.  
  1202.       +o Regular Expression
  1203.            s'$lhs'$rhs' now    does no    interpolation on either    side.
  1204.            It used to interpolate $lhs but not $rhs.  (And still
  1205.            does not    match a    literal    '$' in string)
  1206.  
  1207.            $a=1;$b=2;
  1208.            $string = '1    2 $a $b';
  1209.            $string =~ s'$a'$b';
  1210.            print $string,"\n";
  1211.  
  1212.            # perl4 prints: $b 2    $a $b
  1213.            # perl5 prints: 1 2 $a $b
  1214.  
  1215.  
  1216.       +o Regular Expression
  1217.            m//g now    attaches its state to the searched string
  1218.            rather than the regular expression.  (Once the scope of
  1219.            a block is left for the sub, the    state of the searched
  1220.            string is lost)
  1221.  
  1222.            $_ =    "ababab";
  1223.            while(m/ab/g){
  1224.                &doit("blah");
  1225.            }
  1226.            sub doit{local($_) =    shift; print "Got $_ "}
  1227.  
  1228.            # perl4 prints: blah    blah blah
  1229.            # perl5 prints: infinite loop blah...
  1230.  
  1231.  
  1232.       +o Regular Expression
  1233.            Currently, if you use the m//o qualifier    on a regular
  1234.            expression within an anonymous sub, _a_l_l closures
  1235.            generated from that anonymous sub will use the regular
  1236.            expression as it    was compiled when it was used the very
  1237.            first time in any such closure.    For instance, if you
  1238.            say
  1239.  
  1240.            sub build_match {
  1241.                my($left,$right)    = @_;
  1242.                return sub { $_[0] =~ /$left stuff $right/o; };
  1243.            }
  1244.  
  1245.            _b_u_i_l_d__m_a_t_c_h() will always return    a sub which matches
  1246.            the contents of $left and $right    as they    were the _f_i_r_s_t
  1247.            time that _b_u_i_l_d__m_a_t_c_h() was called, not as they are in
  1248.  
  1249.  
  1250.  
  1251.      Page 19                        (printed 10/23/98)
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1259.  
  1260.  
  1261.  
  1262.            the current call.
  1263.  
  1264.            This is probably    a bug, and may change in future
  1265.            versions    of Perl.
  1266.  
  1267.       +o Regular Expression
  1268.            If no parentheses are used in a match, Perl4 sets $+ to
  1269.            the whole match,    just like $&. Perl5 does not.
  1270.  
  1271.            "abcdef" =~ /b.*e/;
  1272.            print "\$+ =    $+\n";
  1273.  
  1274.            # perl4 prints: bcde
  1275.            # perl5 prints:
  1276.  
  1277.  
  1278.       +o Regular Expression
  1279.            substitution now    returns    the null string    if it fails
  1280.  
  1281.            $string = "test";
  1282.            $value = ($string =~    s/foo//);
  1283.            print $value, "\n";
  1284.  
  1285.            # perl4 prints: 0
  1286.            # perl5 prints:
  1287.  
  1288.            Also see    the section on _N_u_m_e_r_i_c_a_l _T_r_a_p_s for another
  1289.            example of this new feature.
  1290.  
  1291.       +o Regular Expression
  1292.            s`lhs`rhs` (using backticks) is now a normal
  1293.            substitution, with no backtick expansion
  1294.  
  1295.            $string = "";
  1296.            $string =~ s`^`hostname`;
  1297.            print $string, "\n";
  1298.  
  1299.            # perl4 prints: <the    local hostname>
  1300.            # perl5 prints: hostname
  1301.  
  1302.  
  1303.       +o Regular Expression
  1304.            Stricter    parsing    of variables used in regular
  1305.            expressions
  1306.  
  1307.            s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o;
  1308.  
  1309.            # perl4: compiles w/o error
  1310.            # perl5: with Scalar    found where operator expected ..., near    "$opt$plus"
  1311.  
  1312.            an added    component of this example, apparently from the
  1313.            same script, is the actual value    of the s'd string
  1314.  
  1315.  
  1316.  
  1317.      Page 20                        (printed 10/23/98)
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1325.  
  1326.  
  1327.  
  1328.            after the substitution.    [$opt] is a character class in
  1329.            perl4 and an array subscript in perl5
  1330.  
  1331.            $grpc = 'a';
  1332.            $opt     = 'r';
  1333.            $_ =    'bar';
  1334.            s/^([^$grpc]*$grpc[$opt]?)/foo/;
  1335.            print ;
  1336.  
  1337.            # perl4 prints: foo
  1338.            # perl5 prints: foobar
  1339.  
  1340.  
  1341.       +o Regular Expression
  1342.            Under perl5, m?x? matches only once, like ?x?. Under
  1343.            perl4, it matched repeatedly, like /x/ or m!x!.
  1344.  
  1345.            $test = "once";
  1346.            sub match { $test =~    m?once?; }
  1347.            &match();
  1348.            if( &match()    ) {
  1349.                # m?x? matches more then    once
  1350.                print "perl4\n";
  1351.            } else {
  1352.                # m?x? matches only once
  1353.                print "perl5\n";
  1354.            }
  1355.  
  1356.            # perl4 prints: perl4
  1357.            # perl5 prints: perl5
  1358.  
  1359.  
  1360.       SSSSuuuubbbbrrrroooouuuuttttiiiinnnneeee,,,, SSSSiiiiggggnnnnaaaallll,,,, SSSSoooorrrrttttiiiinnnngggg TTTTrrrraaaappppssss
  1361.  
  1362.       The general group of Perl4-to-Perl5 traps having to do with
  1363.       Signals, Sorting, and    their related subroutines, as well as
  1364.       general subroutine traps.  Includes some OS-Specific traps.
  1365.  
  1366.       +o (Signals)
  1367.            Barewords that used to look like    strings    to Perl    will
  1368.            now look    like subroutine    calls if a subroutine by that
  1369.            name is defined before the compiler sees    them.
  1370.  
  1371.            sub SeeYa { warn"Hasta la vista, baby!" }
  1372.            $SIG{'TERM'}    = SeeYa;
  1373.            print "SIGTERM is now $SIG{'TERM'}\n";
  1374.  
  1375.            # perl4 prints: SIGTERM is main'SeeYa
  1376.            # perl5 prints: SIGTERM is now main::1
  1377.  
  1378.            Use ----wwww to catch this one
  1379.  
  1380.  
  1381.  
  1382.  
  1383.      Page 21                        (printed 10/23/98)
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1391.  
  1392.  
  1393.  
  1394.       +o (Sort Subroutine)
  1395.            reverse is no longer allowed as the name    of a sort
  1396.            subroutine.
  1397.  
  1398.            sub reverse{    print "yup "; $a <=> $b    }
  1399.            print sort reverse a,b,c;
  1400.  
  1401.            # perl4 prints: yup yup yup yup abc
  1402.            # perl5 prints: abc
  1403.  
  1404.  
  1405.       +o warn() won't let you specify a filehandle.
  1406.            Although    it _always_ printed to STDERR, _w_a_r_n() would
  1407.            let you specify a filehandle in perl4.  With perl5 it
  1408.            does not.
  1409.  
  1410.            warn    STDERR "Foo!";
  1411.  
  1412.            # perl4 prints: Foo!
  1413.            # perl5 prints: String found    where operator expected
  1414.  
  1415.  
  1416.       OOOOSSSS TTTTrrrraaaappppssss
  1417.  
  1418.       +o (SysV)
  1419.            Under HPUX, and some other SysV OSes, one had to    reset
  1420.            any signal handler, within  the signal handler
  1421.            function, each time a signal was    handled    with perl4.
  1422.            With perl5, the reset is    now done correctly.  Any code
  1423.            relying on the handler _not_ being reset    will have to
  1424.            be reworked.
  1425.  
  1426.            Since version 5.002, Perl uses _s_i_g_a_c_t_i_o_n() under    SysV.
  1427.  
  1428.            sub gotit {
  1429.                print "Got @_...    ";
  1430.            }
  1431.            $SIG{'INT'} = 'gotit';
  1432.  
  1433.            $| =    1;
  1434.            $pid    = fork;
  1435.            if ($pid) {
  1436.                kill('INT', $pid);
  1437.                sleep(1);
  1438.                kill('INT', $pid);
  1439.            } else {
  1440.                while (1) {sleep(10);}
  1441.            }
  1442.  
  1443.            # perl4 (HPUX) prints: Got INT...
  1444.            # perl5 (HPUX) prints: Got INT... Got INT...
  1445.  
  1446.  
  1447.  
  1448.  
  1449.      Page 22                        (printed 10/23/98)
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1457.  
  1458.  
  1459.  
  1460.       +o (SysV)
  1461.            Under SysV OSes,    seek() on a file opened    to append >>
  1462.            now does    the right thing    w.r.t. the _f_o_p_e_n() manpage.
  1463.            e.g., - When a file is opened for append,  it  is
  1464.            impossible to overwrite information already in the
  1465.            file.
  1466.  
  1467.            open(TEST,">>seek.test");
  1468.            $start = tell TEST ;
  1469.            foreach(1 ..    9){
  1470.                print TEST "$_ ";
  1471.            }
  1472.            $end    = tell TEST ;
  1473.            seek(TEST,$start,0);
  1474.            print TEST "18 characters here";
  1475.  
  1476.            # perl4 (solaris) seek.test has: 18 characters here
  1477.            # perl5 (solaris) seek.test has: 1 2    3 4 5 6    7 8 9 18 characters here
  1478.  
  1479.  
  1480.       IIIInnnntttteeeerrrrppppoooollllaaaattttiiiioooonnnn    TTTTrrrraaaappppssss
  1481.  
  1482.       Perl4-to-Perl5 traps having to do with how things get
  1483.       interpolated within certain expressions, statements,
  1484.       contexts, or whatever.
  1485.  
  1486.       +o Interpolation
  1487.            @ now always interpolates an array in double-quotish
  1488.            strings.
  1489.  
  1490.            print "To: someone@somewhere.com\n";
  1491.  
  1492.            # perl4 prints: To:someone@somewhere.com
  1493.            # perl5 errors : In string, @somewhere now must be written as \@somewhere
  1494.  
  1495.  
  1496.       +o Interpolation
  1497.            Double-quoted strings may no longer end with an
  1498.            unescaped $ or @.
  1499.  
  1500.            $foo    = "foo$";
  1501.            $bar    = "bar@";
  1502.            print "foo is $foo, bar is $bar\n";
  1503.  
  1504.            # perl4 prints: foo is foo$,    bar is bar@
  1505.            # perl5 errors: Final $ should be \$    or $name
  1506.  
  1507.            Note: perl5 DOES    NOT error on the terminating @ in $bar
  1508.  
  1509.       +o Interpolation
  1510.            Perl now    sometimes evaluates arbitrary expressions
  1511.            inside braces that occur    within double quotes (usually
  1512.  
  1513.  
  1514.  
  1515.      Page 23                        (printed 10/23/98)
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1523.  
  1524.  
  1525.  
  1526.            when the    opening    brace is preceded by $ or @).
  1527.  
  1528.            @www    = "buz";
  1529.            $foo    = "foo";
  1530.            $bar    = "bar";
  1531.            sub foo { return "bar" };
  1532.            print "|@{w.w.w}|${main'foo}|";
  1533.  
  1534.            # perl4 prints: |@{w.w.w}|foo|
  1535.            # perl5 prints: |buz|bar|
  1536.  
  1537.            Note that you can use strict; to    ward off such
  1538.            trappiness under    perl5.
  1539.  
  1540.       +o Interpolation
  1541.            The construct "this is $$x" used    to interpolate the pid
  1542.            at that point, but now apparently tries to dereference
  1543.            $x.  $$ by itself still works fine, however.
  1544.  
  1545.            print "this is $$x\n";
  1546.  
  1547.            # perl4 prints: this    is XXXx      (XXX is the current pid)
  1548.            # perl5 prints: this    is
  1549.  
  1550.  
  1551.       +o Interpolation
  1552.            Creation    of hashes on the fly with eval "EXPR" now
  1553.            requires    either both $'s    to be protected    in the
  1554.            specification of    the hash name, or both curlies to be
  1555.            protected.  If both curlies are protected, the result
  1556.            will be compatible with perl4 and perl5.     This is a
  1557.            very common practice, and should    be changed to use the
  1558.            block form of eval{}  if    possible.
  1559.  
  1560.            $hashname = "foobar";
  1561.            $key    = "baz";
  1562.            $value = 1234;
  1563.            eval    "\$$hashname{'$key'} = q|$value|";
  1564.            (defined($foobar{'baz'})) ?    (print "Yup") :    (print "Nope");
  1565.  
  1566.            # perl4 prints: Yup
  1567.            # perl5 prints: Nope
  1568.  
  1569.            Changing
  1570.  
  1571.            eval    "\$$hashname{'$key'} = q|$value|";
  1572.  
  1573.            to
  1574.  
  1575.            eval    "\$\$hashname{'$key'} =    q|$value|";
  1576.  
  1577.            causes the following result:
  1578.  
  1579.  
  1580.  
  1581.      Page 24                        (printed 10/23/98)
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1589.  
  1590.  
  1591.  
  1592.            # perl4 prints: Nope
  1593.            # perl5 prints: Yup
  1594.  
  1595.            or, changing to
  1596.  
  1597.            eval    "\$$hashname\{'$key'\} = q|$value|";
  1598.  
  1599.            causes the following result:
  1600.  
  1601.            # perl4 prints: Yup
  1602.            # perl5 prints: Yup
  1603.            # and is compatible for both    versions
  1604.  
  1605.  
  1606.       +o Interpolation
  1607.            perl4 programs which unconsciously rely on the bugs in
  1608.            earlier perl versions.
  1609.  
  1610.            perl    -e '$bar=q/not/; print "This is    $foo{$bar} perl5"'
  1611.  
  1612.            # perl4 prints: This    is not perl5
  1613.            # perl5 prints: This    is perl5
  1614.  
  1615.  
  1616.       +o Interpolation
  1617.            You also    have to    be careful about array references.
  1618.  
  1619.            print "$foo{"
  1620.  
  1621.            perl    4 prints: {
  1622.            perl    5 prints: syntax error
  1623.  
  1624.  
  1625.       +o Interpolation
  1626.            Similarly, watch    out for:
  1627.  
  1628.            $foo    = "array";
  1629.            print "\$$foo{bar}\n";
  1630.  
  1631.            # perl4 prints: $array{bar}
  1632.            # perl5 prints: $
  1633.  
  1634.            Perl 5 is looking for $array{bar} which doesn't exist,
  1635.            but perl    4 is happy just    to expand $foo to "array" by
  1636.            itself.    Watch out for this especially in eval's.
  1637.  
  1638.       +o Interpolation
  1639.            qq() string passed to eval
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.  
  1647.      Page 25                        (printed 10/23/98)
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1655.  
  1656.  
  1657.  
  1658.            eval    qq(
  1659.                foreach \$y (keys %\$x\)    {
  1660.                \$count++;
  1661.                }
  1662.            );
  1663.  
  1664.            # perl4 runs    this ok
  1665.            # perl5 prints: Can't find string terminator    ")"
  1666.  
  1667.  
  1668.       DDDDBBBBMMMM TTTTrrrraaaappppssss
  1669.  
  1670.       General DBM traps.
  1671.  
  1672.       +o DBM
  1673.            Existing    dbm databases created under perl4 (or any
  1674.            other dbm/ndbm tool) may    cause the same script, run
  1675.            under perl5, to fail.  The build    of perl5 must have
  1676.            been linked with    the same dbm/ndbm as the default for
  1677.            dbmopen() to function properly without tie'ing to an
  1678.            extension dbm implementation.
  1679.  
  1680.            dbmopen (%dbm, "file", undef);
  1681.            print "ok\n";
  1682.  
  1683.            # perl4 prints: ok
  1684.            # perl5 prints: ok (IFF linked with -ldbm or    -lndbm)
  1685.  
  1686.  
  1687.       +o DBM
  1688.            Existing    dbm databases created under perl4 (or any
  1689.            other dbm/ndbm tool) may    cause the same script, run
  1690.            under perl5, to fail.  The error    generated when
  1691.            exceeding the limit on the key/value size will cause
  1692.            perl5 to    exit immediately.
  1693.  
  1694.            dbmopen(DB, "testdb",0600) || die "couldn't open db!    $!";
  1695.            $DB{'trap'} = "x" x 1024;  #    value too large    for most dbm/ndbm
  1696.            print "YUP\n";
  1697.  
  1698.            # perl4 prints:
  1699.            dbm store returned -1, errno    28, key    "trap" at - line 3.
  1700.            YUP
  1701.  
  1702.            # perl5 prints:
  1703.            dbm store returned -1, errno    28, key    "trap" at - line 3.
  1704.  
  1705.  
  1706.       UUUUnnnnccccllllaaaassssssssiiiiffffiiiieeeedddd TTTTrrrraaaappppssss
  1707.  
  1708.       Everything else.
  1709.  
  1710.  
  1711.  
  1712.  
  1713.      Page 26                        (printed 10/23/98)
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1721.  
  1722.  
  1723.  
  1724.       +o require/do trap using returned value
  1725.            If the file doit.pl has:
  1726.  
  1727.            sub foo {
  1728.                $rc = do    "./do.pl";
  1729.                return 8;
  1730.            }
  1731.            print &foo, "\n";
  1732.  
  1733.            And the do.pl file has the following single line:
  1734.  
  1735.            return 3;
  1736.  
  1737.            Running doit.pl gives the following:
  1738.  
  1739.            # perl 4 prints: 3 (aborts the subroutine early)
  1740.            # perl 5 prints: 8
  1741.  
  1742.            Same behavior if    you replace do with require.
  1743.  
  1744.       +o split on empty string with LIMIT specified
  1745.  
  1746.                $string = '';
  1747.            @list = split(/foo/,    $string, 2)
  1748.  
  1749.            Perl4 returns a one element list    containing the empty
  1750.            string but Perl5    returns    an empty list.
  1751.  
  1752.       As always, if    any of these are ever officially declared as
  1753.       bugs,    they'll    be fixed and removed.
  1754.  
  1755.  
  1756.  
  1757.  
  1758.  
  1759.  
  1760.  
  1761.  
  1762.  
  1763.  
  1764.  
  1765.  
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779.      Page 27                        (printed 10/23/98)
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.      PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLTTTTRRRRAAAAPPPP((((1111))))
  1787.  
  1788.  
  1789.  
  1790.  
  1791.  
  1792.  
  1793.  
  1794.  
  1795.  
  1796.  
  1797.  
  1798.  
  1799.  
  1800.  
  1801.  
  1802.  
  1803.  
  1804.  
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810.  
  1811.  
  1812.  
  1813.  
  1814.  
  1815.  
  1816.  
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823.  
  1824.  
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.      Page 28                        (printed 10/23/98)
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.